//
//  Pet_DeleteViewController.swift
//  PetApp
//
//  Created by mac on 2023/9/18.
//  Copyright © 2023 Pet_. All rights reserved.
//

import UIKit

class Pet_DeleteViewController: BasicViewController,UICollectionViewDelegate,UICollectionViewDataSource {
    
    private var titleL1 = UILabel.initV(.setTypeFace(18.Fit, type: .medium), text: "Why not use?", textColor: .black)
    private var titleL2 = UILabel.initV(.setTypeFace(18.Fit, type: .medium), text: "Describe", textColor: .black)

    private var selectedIndex: Int?
    private lazy var reasonView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 10.Fit
        layout.minimumInteritemSpacing = 0.Fit
        layout.itemSize = .init(width: screenWidth/2 - 20, height: 40.Fit)
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.register(Delete_FeedBack_cell_Reason_Item.self, forCellWithReuseIdentifier: Delete_FeedBack_cell_Reason_Item.description())
        view.backgroundColor = .clear
        view.delegate = self
        view.dataSource = self
        return view
    }()

    private lazy var descView: Custom_TextView = {
        let view = Custom_TextView()
        view.backgroundColor = UIColor.white
        view.textContainerInset = UIEdgeInsets(top: 10.Fit, left: 10.Fit, bottom: 10.Fit, right: 10.Fit)
        view.tintColor = mainColor
        view.textColor = mainColor
        view.placeHolderColor = UIColor.color(hexString: "#999999",alpha: 1)
        view.placeHolder = "Tell us how we can do better"
        view.returnKeyType = .done
        view.font = .setTypeFace(13.Fit,type: .medium)
        return view
    }()
    lazy var doneBtn: UIButton = {
        let l = UIButton(frame: .zero)
        l.backgroundColor = .black
        l.titleLabel?.font = .setTypeFace(16.Fit,type: .medium)
        l.setTitleColor(mainColor, for: .normal)
        l.setTitle("Delete Account", for: .normal)
        return l
    }()
    private var tipLabel = UILabel.initV(.setTypeFace(13.Fit, type: .medium), text: "All your account information will be Deleted please consider carefully!", textColor: .color(hexString: "#ED3434"))
    private var dataSource: [String] = ["App bugs", "Bad user behavior", "No interests"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "Delete Account"
        view.subviews {
            titleL1
            reasonView
            titleL2
            descView
            doneBtn
            tipLabel
        }
        
        titleL1.left(20.Fit).right(20.Fit).height(40.Fit).top(NavHeight)
        
        reasonView.left(15.Fit).right(15.Fit).height(90.Fit)
        reasonView.Top == titleL1.Bottom + 12.Fit
        
        titleL2.left(20.Fit).right(20.Fit).height(40.Fit)
        titleL2.Top == reasonView.Bottom + 12.Fit

        descView.left(15.Fit).right(15.Fit).height(120.Fit)
        descView.Top == titleL2.Bottom + 12.Fit
        descView.setAllCorners(corner: 20.Fit)
        
        doneBtn.left(20.Fit).height(50.Fit).right(20.Fit)
        doneBtn.Top == descView.Bottom + 15.Fit
        doneBtn.setAllCorners(corner: 25.Fit)
        doneBtn.addAct {
            RT.deletUserWith { str in
                AgoraRTMClient.shared._kit?.logout()
                UserDefaults.standard.setValue(nil, forKey: "UserInfo")
                UserDefaults.standard.setValue(nil, forKey: USERINFOKEY)
                // 登录界面
                let vc = Pet_LoginHomeViewController()
                UIApplication.shared.keyWindow?.rootViewController = NavigationInterface(rootViewController: vc)
            }
        }
        
        
        tipLabel.textAlignment = .center
        tipLabel.left(20.Fit).right(20.Fit).height(40.Fit)
        tipLabel.Top == doneBtn.Bottom + 12.Fit

    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let item = collectionView.dequeueReusableCell(withReuseIdentifier: Delete_FeedBack_cell_Reason_Item.description(), for: indexPath) as! Delete_FeedBack_cell_Reason_Item
        item.title = dataSource[indexPath.row]
        item.isSel = selectedIndex == indexPath.row
        return item
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectedIndex = indexPath.row
        collectionView.reloadData()
    }

}


class Delete_FeedBack_cell_Reason_Item: UICollectionViewCell {
    
    var title: String? {
        didSet {
            if let name = title {
                reasonBtn.setTitle(name, for: .normal)
            }
        }
    }
    var isSel: Bool? {
        didSet {
            reasonBtn.removeGradient()
            if let sel = isSel {
                reasonBtn.isSelected = sel
                if sel {
                    reasonBtn.backgroundColor = UIColor.black
                    reasonBtn.setTitleColor(.white, for: .normal)
                }else {
                    reasonBtn.backgroundColor = .white
                    reasonBtn.setTitleColor(.black, for: .normal)
                }
            }else {
                reasonBtn.backgroundColor = .white
                reasonBtn.setTitleColor(.black, for: .normal)
            }
        }
    }
    
    private lazy var reasonBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.clipsToBounds = true
        btn.layer.cornerRadius = 20.Fit
        btn.setTitleColor(UIColor.color(hexString: "#73657B"), for: .normal)
        btn.setTitleColor(mainColor, for: .selected)
        btn.titleLabel?.font = .setTypeFace(16, type: .medium)
        btn.setBackgroundImage(UIImage(), for: .normal)
        btn.isUserInteractionEnabled = false
        btn.titleLabel?.numberOfLines = 0
        btn.titleLabel?.textAlignment = .center
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(reasonBtn)
        reasonBtn.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
